// src/main/java/com/lyj/controller/FamilyController.java
package com.lyj.controller;

import com.lyj.pojo.Result;
import com.lyj.pojo.dto.FamilyUpdateNameDTO;
import com.lyj.pojo.vo.RoleUpdateDTO;
import com.lyj.service.FamilyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/family")
public class FamilyController {

    @Autowired
    private FamilyService familyService;

    /**
     * 生成家庭邀请码（管理员专用）
     */
    @PostMapping("/generate-share-code")
    public Result generateShareCode(@RequestParam Integer userId) {
        String shareCode = familyService.generateShareCode(userId);
        Map<String, String> result = new HashMap<>();
        result.put("shareCode", shareCode);
        return Result.success(result);
    }

    /**
     * 修改家庭成员角色（管理员专用）
     */
    @PutMapping("/member/role")
    public Result updateMemberRole(@RequestParam Integer operatorId, @RequestBody RoleUpdateDTO roleUpdateDTO) {
        familyService.updateMemberRole(operatorId, roleUpdateDTO.getMemberId(), roleUpdateDTO.getRole());
        return Result.success();
    }

    /**
     * 删除家庭成员（管理员专用）
     */
    @DeleteMapping("/member/{memberId}")
    public Result deleteMember(@RequestParam Integer operatorId, @PathVariable Integer memberId) {
        familyService.deleteMember(operatorId, memberId);
        return Result.success();
    }

    @PutMapping("/update-name")
    public Result updateFamilyName(
            @RequestParam Integer operatorId,
            @Valid @RequestBody FamilyUpdateNameDTO dto) {
        familyService.updateFamilyName(operatorId, dto);
        return Result.success("家庭名称更新成功");
    }
}
